﻿using System;
using System.Text.RegularExpressions;
using System.Web.Security;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using MembershipUser = CCSManager.CCS.MembershipUser;

namespace CCSManager
{
    /// <summary>
    /// Interaction logic for Register.xaml
    /// </summary>
    public partial class Register : UserControl
    {
        public Register()
        {
            InitializeComponent();
#if DEBUG
            userNameTextBox.Text = "Test_" + DateTime.Now.Ticks;
            emailTextBox.Text = "test_" + DateTime.Now.Ticks + "@domain.com";
            passwordTextBox.Password = "passw0rd!";
            confirmPasswordTextBox.Password = "passw0rd!";
#endif
        }

        public event EventHandler Registered;

        private void registerButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (passwordTextBox.Password != confirmPasswordTextBox.Password)
                {
                    throw new MembershipCreateUserException("Password and Confirm Password must match");
                }
                var webService = new CCS.CCS();
                var paymentSettings = new CCS.CCSPaymentSettings
                                          {
                                              Payment_URL = Properties.Settings.Default.PaymentURL,
                                              PayPal_AccountEmail = emailTextBox.Text,
                                              PayPal_cancelURL = string.Empty,
                                              PayPal_currencyCode = "USD",
                                              PayPal_includeNote = false,
                                              PayPal_ItemNumber = Guid.NewGuid().ToString(),
                                              PayPal_logoURL = string.Empty,
                                              PayPal_noteTitle = string.Empty,
                                              PayPal_notifyURL = Properties.Settings.Default.PaymentNotifyURL,
                                              PayPal_Quantity = 1,
                                              PayPal_requireShippingAddress = false,
                                              PayPal_successUrl = string.Empty,
                                              PayPal_tag = string.Empty,
                                              Timestamp = DateTime.Now
                                          };
                CCS.MembershipUser membershipUser = webService.CreateUser(userNameTextBox.Text, passwordTextBox.Password, paymentSettings);
                if (membershipUser != null)
                {
                    if (Registered != null)
                    {
                        Registered(this, new EventArgs());
                    }
                }
            }
            catch (Exception exception)
            {
                Match membershipExceptionMatch = Regex.Match(exception.Message, "(?<=System\\.Web\\.Security\\.MembershipCreateUserException\\:\\s).*\\.+");
                if (!string.IsNullOrEmpty(membershipExceptionMatch.Value))
                {
                    informationTextBlock.Text = string.Format("There was an error in registration: {0}", membershipExceptionMatch.Value);
                    informationTextBlock.Foreground = new SolidColorBrush(Color.FromRgb(255, 0, 0));
                }
            }
        }
    }
}